/*
 * @ : Copyright (c) 2021 Phytium Information Technology, Inc. 
 *  
 * SPDX-License-Identifier: Apache-2.0.
 * 
 * @Date: 2021-09-01 15:01:13
 * @LastEditTime: 2021-09-01 17:30:21
 * @Description:  This files is for 
 * 
 * @Modify History: 
 *  Ver   Who        Date         Changes
 * ----- ------     --------    --------------------------------------
 */
#include <algorithm>
#include <string>
#include <ostream>
#include <sstream>
#include <iomanip>
#include "../elfio/elfio_dump.hpp"
#include "../elfio/elfio.hpp"
#include "elftool.h"

using namespace ELFIO;
using namespace std;

static void ElfIoDumpParse(ElfParseInfo &info)
{
    cout << "ELF Header" << endl
         << endl
         << "  Class:      " << (int)(info.file_class) << endl
         << "  Encoding:   " << (int)info.encoding
         << endl
         //<< "  ELFVersion: " << str_version( reader.get_elf_version() )
         //<< endl
         << "  Type:       " << (int)info.elf_type << endl
         << "  Machine:    " << (int)info.machine_type
         << endl
         //<< "  Version:    " << str_version( reader.get_version() )
         //<< endl
         << "  Entry:      "
         << "0x" << hex << info.entry_addr << endl
         //<< "  Flags:      "
         //<< "0x" << hex << reader.get_flags() << endl
         << endl;
}

int ElfIoParse( string &elf_file, ElfParseInfo &info )
{
    // Create an elfio reader
    elfio reader;

    // Load ELF data
    if (!reader.load( elf_file ))
    {
        cout << "Can't find or process ELF file " << elf_file << endl;
        return 2;
    }

    info.file_class = reader.get_class();
    info.encoding = reader.get_encoding();
    info.os_abi = reader.get_os_abi();
    info.elf_type = reader.get_type();
    info.machine_type = reader.get_machine();
    info.entry_addr = reader.get_entry();

    ElfIoDumpParse(info);

    return 0;
}